<?

class RequestRenderer extends TRepeaterItemRenderer
{

	public function onInit($param)
    {
		if(!$this->getPage()->isNavigatedUserSameAsOwner())
		{
			$this->btnSend->Visible = false;
		}
	}
	public function getFriendName($friend_id)
	{
		$owner = UserRecord::finder()->findByPk($friend_id);
		//echo "sssss ". $this;
		//$u_id = $owner->user_id;
		return $owner->username;
	}
	
	public function buttonClicked($sender, $param)
    {
       // echo "sdsdssdsd ". $this->hidden_id->Value ;
		$this->addFriends();
		$this->getPage()->onInit($param); // refresh the ProfileUser.tpl
    }
	
	public function addFriends()
	{
		$id = $this->hidden_id->Value;
		$rec = FriendReqRecord::finder()->findByPk($id);
		
		// add as friendRecord
		$newRec = new FriendRecord();
	
		$newRec->user_id = $rec->user_id;
		$newRec->friend_id = $rec->friend_id;	
		$newRec->friends_datetime = date("Y/m/d H:i:s");
		$newRec->save();	
		
		$this->addFriendsActivity($newRec);
		// delete the friend request record
		FriendReqRecord::finder()->deleteByPk($id);
	}
	
	public function addFriendsActivity($newRec)
	{
		$newAct = new ActivityRecord();
		//$newAct->user_id = $rec->user_id;
		$newAct->activity_id = 0; // add friends
		//$newAct->friend_id = $rec->friend_id;
		$newAct->rec_id = $newRec->id;
		$newAct->actor_id = $newRec->user_id;
		$newAct->dotouser_id = $newRec->friend_id;
		$newAct->save();
	}
	
}

?>